import { _decorator, BoxCollider, Component, Enum, Material, MeshRenderer, Node, Vec4 } from 'cc';
import { BlockDirection, GateDirection, ModelColor } from './enum';
import { GridController } from './GridController';
import EventBus from './EventBus';
import { EventConst } from './EvenetConst';
const { ccclass, property, executeInEditMode } = _decorator;

@ccclass('Blockl')
@executeInEditMode(true)
export class Block extends Component {


    @property({ type: Enum(BlockDirection) })
    private _blockDirection: BlockDirection = BlockDirection.TOP;

    @property({ type: Enum(BlockDirection) })
    get blockDirection(): BlockDirection {
        return this._blockDirection;
    }
    set blockDirection(value: BlockDirection) {
        const from = this._blockDirection;
        this._blockDirection = value;
        this.updateBlockDirection(value);
    }


    @property(GridController)
    gridController: GridController;

    @property([Material])
    material: Material[] = [];

    @property({ type: Enum(ModelColor) })
    _modelColor: ModelColor = ModelColor.BlUE;

    @property({ type: Enum(ModelColor) })
    get modelColor(): ModelColor {
        return this._modelColor;
    }

    set modelColor(value: ModelColor) {
        this._modelColor = value;
        this.updateModelColor()
    }


    start() {
        // console
        this.updateModelColor();
        EventBus.on(EventConst.EliminationEnd, this.onUnscheduleHighLight, this);
        EventBus.on(EventConst.EliminationEnd, this.onUnscheduleHighLight, this);
        EventBus.on(EventConst.PrepareClickEliminationBlock, this.onScheduleHighLight, this);
        EventBus.on(EventConst.PrepareClickEliminationBlockColor, this.onScheduleHighLight, this);
    }
    onDestroy() {
        EventBus.off(EventConst.PrepareClickEliminationBlock, this.onScheduleHighLight, this);
        EventBus.off(EventConst.PrepareClickEliminationBlockColor, this.onScheduleHighLight, this);
    }
    //start block高亮
    isHightLight: boolean = false;
    onUnscheduleHighLight() {
        this.node.getChildByName('BlockMesh').getComponent(MeshRenderer).getMaterialInstance(0).setProperty('brightness', 0);
        this.unschedule(this.blockHighLight)
    }
    onScheduleHighLight() {
        this.schedule(this.blockHighLight, 0.5, Infinity)
    }
    blockHighLight() {
        if (this.isHightLight) {
            this.node.getChildByName('BlockMesh').getComponent(MeshRenderer).getMaterialInstance(0).setProperty('brightness', 0);
            this.isHightLight = false;
        } else {
            this.node.getChildByName('BlockMesh').getComponent(MeshRenderer).getMaterialInstance(0).setProperty('brightness', 0.1);
            this.isHightLight = true;
        }
    }
    //end block 高亮代码结束


    updateModelColor() {
        if (this.modelColor == ModelColor.RED) {
            this.node.getChildByName('BlockMesh').getComponent(MeshRenderer).material = this.material[0];
        } else if (this.modelColor == ModelColor.BlUE) {
            this.node.getChildByName('BlockMesh').getComponent(MeshRenderer).material = this.material[1];
        }
    }

    getBlockWH() {
        return {
            width: 4,
            height: 4
        }
    }

    setMatertialClipPlane(dir: GateDirection) {
        switch (dir) {
            case GateDirection.TOP:
                this.node.getChildByName('BlockMesh').getComponent(MeshRenderer).getMaterialInstance(0).setProperty('clipPlane', new Vec4(0, 0, -1, this.gridController.sizeGrid.z + 1));
                break;
            case GateDirection.LEft:
                this.node.getChildByName('BlockMesh').getComponent(MeshRenderer).getMaterialInstance(0).setProperty('clipPlane', new Vec4(-1, 0, 0, this.gridController.sizeGrid.x - 1));
                break;
            case GateDirection.Bottom:
                this.node.getChildByName('BlockMesh').getComponent(MeshRenderer).getMaterialInstance(0).setProperty('clipPlane', new Vec4(0, 0, 1, this.gridController.sizeGrid.z - 1));
                break;
            case GateDirection.Right:
                this.node.getChildByName('BlockMesh').getComponent(MeshRenderer).getMaterialInstance(0).setProperty('clipPlane', new Vec4(1, 0, 0, this.gridController.sizeGrid.x + 1));
                break;
            default:
                break;
        }

    }
    //控制方块的朝向
    updateBlockDirection(dir?: BlockDirection) {
        // 如果提供了 from/to，则以 to 的绝对角度为准；否则使用当前 _BlockDirection
        const targetDir = (dir !== undefined ? dir : this.blockDirection);
        let angleY: number = 0;
        console.log(targetDir, 'targetDir')
        switch (targetDir) {
            case BlockDirection.TOP:
                angleY = 0;
                break;
            case BlockDirection.RIGHT:
                angleY = -90;
                break;
            case BlockDirection.LEFT:
                angleY = 90;
                break;
            case BlockDirection.BOTTOM:
                angleY = 180;
                break;
            default:
                angleY = 0;
                break;
        }
        console.log(angleY, 'angleY')
        this.node.setRotationFromEuler(-90, angleY, 0);
    }
    update(deltaTime: number) {

    }
}


